﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
using System.ComponentModel;

namespace FCNS.Controls
{
    /// <summary>
    /// 将 ProgressBarPlus 添加到 StatusStrip
    /// </summary>
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
    [ToolboxBitmap(typeof(System.Windows.Forms.ProgressBar))]
    public class ProgressBarPlusInToolStrip : ToolStripControlHost
    {
        #region 属性
        /// <summary>
        /// 获取 ProgressBarPlus 控件
        /// </summary>
        [Browsable(false)]
        public ProgressBarPlus ProgressBarPlus
        {
            get
            {
                return base.Control as ProgressBarPlus;
            }
        }

        public override RightToLeft RightToLeft
        {
            get
            {
                return this.ProgressBarPlus.RightToLeft;
            }
            set
            {
                this.ProgressBarPlus.RightToLeft = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的背景颜色
        /// </summary>
        [Category("FCNS"), Description("控件背景颜色")]
        public Color BackgroundColor
        {
            get
            {
                return this.ProgressBarPlus.BackgroundColor;
            }
            set
            {
                this.ProgressBarPlus.BackgroundColor = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的进度颜色
        /// </summary>
        [Category("FCNS"), Description("控件的进度颜色")]
        public Color ValueColor
        {
            get
            {
                return this.ProgressBarPlus.ValueColor;
            }
            set
            {
                this.ProgressBarPlus.ValueColor = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的前景颜色
        /// </summary>
        [Category("FCNS"), Description("控件的前景颜色")]
        public override Color ForeColor
        {
            get
            {
                return this.ProgressBarPlus.ForeColor;
            }
            set
            {
                this.ProgressBarPlus.ForeColor = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的字体
        /// </summary>
        [Category("FCNS"), Description("控件的字体")]
        public override Font Font
        {
            get
            { 
                return this.ProgressBarPlus.Font; 
            }
            set 
            {
                this.ProgressBarPlus.Font = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的值上限
        /// </summary>
        [Category("FCNS"), Description("值上限")]
        [DefaultValue(100)]
        [RefreshProperties(RefreshProperties.All)]
        public int Maximum
        {
            get
            {
                return this.ProgressBarPlus.Maximum;
            }
            set
            {
                this.ProgressBarPlus.Maximum = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的值下限
        /// </summary>
        [Category("FCNS"), Description("值下限")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int Minimum
        {
            get
            {
                return this.ProgressBarPlus.Minimum;
            }
            set
            {
                this.ProgressBarPlus.Minimum = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的值
        /// </summary>
        [Category("FCNS"), Description("值")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int Value
        {
            get
            {
                return this.ProgressBarPlus.Value;
            }
            set
            {
                this.ProgressBarPlus.Value = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的文本
        /// </summary>
        [Category("FCNS"), Description("文本")]
        public override string Text
        {
            get
            {
                return this.ProgressBarPlus.Text;
            }
            set
            {
                this.ProgressBarPlus.Text = value;
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(100, 15); 
            }
        }

        protected override Padding DefaultMargin
        {
            get
            {
                if ((base.Owner != null) && (base.Owner is StatusStrip))
                {
                    return new Padding(1, 3, 1, 3);
                }
                return new Padding(1, 1, 1, 2);
            }
        }
        #endregion

        public ProgressBarPlusInToolStrip()
            : base(CreateControlInstance())
        {
        }

        protected override void OnOwnerChanged(EventArgs e)
        {
            if (base.Owner != null)
            {
                base.Owner.RendererChanged += new EventHandler(OwnerRendererChanged);
            }
            base.OnOwnerChanged(e);
        }

        private static Control CreateControlInstance()
        {
            FCNS.Controls.ProgressBarPlus progressBar = new FCNS.Controls.ProgressBarPlus();
            progressBar.Size = new Size(100, 15);

            return progressBar;
        }
        private void OwnerRendererChanged(object sender, EventArgs e)
        {
            ToolStripRenderer toolsTripRenderer = this.Owner.Renderer;
            if (toolsTripRenderer != null)
            {
                this.Margin = DefaultMargin;
            }
        }
    }
}